package com.stenio.fancat.service.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.shop.FcShopFlea;
import com.stenio.fancat.domain.shop.FcShopImage;
import com.stenio.fancat.form.shop.ShopFleaForm;
import com.stenio.fancat.mapper.shop.FcShopFleaMapper;
import com.stenio.fancat.service.enums.ImageType;
import com.stenio.fancat.service.enums.SaleStatus;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ShopFleaService extends ServiceImpl<FcShopFleaMapper, FcShopFlea> {

	@Autowired
	private FcShopFleaMapper fcShopFleaMapper;

	@Autowired
	private ShopImageService shopImageService;

	private static final Logger logger = LoggerFactory.getLogger(ShopFleaService.class);

	/**
	 * 保存商品
	 * @param form
	 * @return
	 */
	public FcShopFlea saveWare(ShopFleaForm form){
		FcShopFlea shop = new FcShopFlea();
		BeanUtils.copyProperties(form,shop);
		Date date = new Date();
		shop.setCreateTime(date);
		shop.setUpdateTime(date);
		shop.setStatus(SaleStatus.OFF.getStatus());
		fcShopFleaMapper.insert(shop);

		//处理关联图片的逻辑
		if(CollectionUtils.isNotEmpty(form.getWareImages())){
			form.getWareImages().stream().forEach(vo->{
				shopImageService.updateBiz(shop.getId(),vo);
			});
		}
		return shop;
	}

	/**
	 * 更新商品
	 * @param form
	 */
	public FcShopFlea updateWare(ShopFleaForm form){
		FcShopFlea shop = new FcShopFlea();
		BeanUtils.copyProperties(form,shop);
		fcShopFleaMapper.updateAll(shop);
		//处理关联图片的逻辑
		FcShopImage shopImage = new FcShopImage();
		shopImage.setType(ImageType.WARE_FLEA.getType());
		shopImage.setBusinessId(form.getId());
		List<FcShopImage> relatedImages = new ArrayList<>();
		relatedImages.addAll(shopImageService.selectByType(shopImage));
		//对于已经建立过关联的记录
		List<Integer> relatedIds = new ArrayList<>();
		relatedImages.stream().forEach(vo->{
			//前端记录中不存在，则删除该存在的记录
			if(!form.getWareImages().contains(vo.getId())){
				shopImageService.deleteById(vo.getId());
			}
			relatedIds.add(vo.getId());
		});
		//对于前端传入的记录
		if(CollectionUtils.isNotEmpty(form.getWareImages())){
			form.getWareImages().stream().forEach(vo->{
				//若已关联的记录中，不存在该记录，则建立关联，否则保持不动
				if(!relatedIds.contains(vo)){
					shopImageService.updateBiz(shop.getId(),vo);
				}
			});
		}
		return shop;
	}

	/**
	 * 商品上下架
	 * @param id
	 * @param status
	 * @throws Exception
	 */
	public FcShopFlea onOff(Integer id, Integer status) throws Exception{
		FcShopFlea shop = this.getById(id);
		SaleStatus temp = SaleStatus.getEnumByStatus(status);
		if(temp==null){
			throw new Exception("上下架状态非法：不是0或1");
		}
		shop.setStatus(status);
		if(status==SaleStatus.ON.getStatus()){
			shop.setOnsaleTime(new Date());
		}
		fcShopFleaMapper.updatePart(shop);
		return shop;
	}



	/**
	 * 根据主键查询
	 * @param id
	 * @return
	 */
	public FcShopFlea getById(Integer id){
		return fcShopFleaMapper.selectById(id);
	}

	/**
	 * 根据条件查询
	 * @param shopForm
	 * @return
	 */
	public Page<FcShopFlea> selectByPage(ShopFleaForm shopForm){
		FcShopFlea fcShop = new FcShopFlea();
		BeanUtils.copyProperties(shopForm,fcShop);
		Page<FcShopFlea> page = new Page<>(shopForm.getPageNo(), shopForm.getPageSize());
		page.setRecords(fcShopFleaMapper.selectByConditionPage(page,fcShop));
		return page;
	}


}
